package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo02 {
    public static void main(String[] args) {

        try {
            ServerSocket serverSocket = new ServerSocket(8888);

            Socket socket = serverSocket.accept();

            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            // 获取数据
            byte[] bytes = new byte[1024];
//            int temp = 0;
//            int length = 0;
//            while ((temp = in.read()) != -1) {
//                bytes[length++] = (byte) temp;
//            }
            int length = in.read(bytes);

            System.out.println(new String(bytes, 0, length));

            String content = "别融化了眼泪，你妆都花了要我怎么记得";
            out.write(content.getBytes());

            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
